#if defined _expire_included
	#endinput
#endif

#define _expire_included

#include < amxmodx >

#define USE_HTTP_REQUEST

#if defined USE_HTTP_REQUEST
#include < sockets >

#define __GMT_TIMEZONE -6

#define __GetYear(%1)	__GetTimeInt("%Y",%1)
#define __GetMonth(%1)	__GetTimeInt("%m",%1)
#define __GetDay(%1)	__GetTimeInt("%d",%1)
#else
#define __GetYear()	__GetTimeInt("%Y")
#define __GetMonth()	__GetTimeInt("%m")
#define __GetDay()	__GetTimeInt("%d")
#endif

stock __GetTimeInt( const szFormat[ ], iTimeStamp = -1 )
{
	new szTime[ 11 ];
	
	if( iTimeStamp == -1 )
	{
		get_time( szFormat, szTime, charsmax( szTime ) );
	}
	else
	{
		format_time( szTime, charsmax( szTime ), szFormat, iTimeStamp );
	}
	
	return str_to_num( szTime );
}

stock CheckExpire( iStartYear, iStartMonth, iStartDay, iStopYear, iStopMonth, iStopDay )
{
#if defined USE_HTTP_REQUEST
	new iError;
	new hSocket = socket_open( "google.com", 80, SOCKET_TCP, iError );
	
	if( hSocket <= 0 || iError )
	{
		return 1;
	}
	
	new const szRequest[ ] = "HEAD / HTTP/1.1^r^nHost: google.com^r^n^r^n";
	
	socket_send( hSocket, szRequest, sizeof( szRequest ) );
	
	while( !socket_change( hSocket ) ) { }
	
	new szData[ 512 ];
	socket_recv( hSocket, szData, charsmax( szData ) );
	
	socket_close( hSocket );
	
	new szOutput[ 10 ][ 128 ];
	new iOutputs = __str_explode( szData, '^n', szOutput, sizeof( szOutput ), charsmax( szOutput[ ] ) );
	
	new szYear[ 5 ], szMonth[ 4 ], szDay[ 3 ], szHour[ 3 ];
	
	for( new i = 0; i < iOutputs; i++ )
	{
		if( equal( szOutput[ i ], "Date: ", 6 ) )
		{
			new iPos = contain( szOutput[ i ], "," );
			if( iPos != -1 )
			{
				copy( szOutput[ i ], charsmax( szOutput[ ] ), szOutput[ i ][ iPos + 2 ] );
			}
			strbreak( szOutput[ i ], szDay, charsmax( szDay ), szOutput[ i ], charsmax( szOutput[ ] ) );
			strbreak( szOutput[ i ], szMonth, charsmax( szMonth ), szOutput[ i ], charsmax( szOutput[ ] ) );
			strbreak( szOutput[ i ], szYear, charsmax( szYear ), szOutput[ i ], charsmax( szOutput[ ] ) );
			strtok( szOutput[ i ], szHour, charsmax( szHour ), szOutput[ i ], charsmax( szOutput[ ] ), ':', 1 );
			
			break;
		}
	}
	
	new iTimeStamp = __mktime( str_to_num( szYear ), __MonthNameToInt( szMonth ), str_to_num( szDay ), str_to_num( szHour ), 0, 0 );
	
	iTimeStamp += ( __GMT_TIMEZONE * 3600 );
	
	new iYear = __GetYear( iTimeStamp );
	new iMonth = __GetMonth( iTimeStamp );
	new iDay = __GetDay( iTimeStamp );
#else
	new iYear = __GetYear( );
	new iMonth = __GetMonth( );
	new iDay = __GetDay( );
#endif
	
	if( iYear == iStartYear )
	{
		if( iMonth == iStartMonth )
		{
			if( iDay < iStartDay )
			{
				return 1;
			}
		}
		else if( iMonth < iStartMonth )
		{
			return 1;
		}
	}
	else if( iYear < iStartYear )
	{
		return 1;
	}
	
	if( iYear == iStopYear )
	{
		if( iMonth == iStopMonth )
		{
			if( iDay > iStopDay )
			{
				return 1;
			}
		}
		else if( iMonth > iStopMonth )
		{
			return 1;
		}
	}
	else if( iYear > iStopYear )
	{
		return 1;
	}
	
	return 0;
}

stock __str_explode(const string[], delimiter, output[][], output_size, output_len)
{
	new i, pos, len = strlen(string);
	
	do
	{
		pos += (copyc(output[i], output_len, string[pos], delimiter) + 1);
	}
	while(pos < len && ++i < output_size);
	
	return i;
}

stock __MonthNameToInt( const szMonth[ ] )
{
	new const szMonthNames[ 12 ][ 4 ] =
	{
		"jan",
		"feb",
		"mar",
		"apr",
		"may",
		"jun",
		"jul",
		"aug",
		"sep",
		"oct",
		"nov",
		"dec"
	};
	
	for( new i = 0; i < 12; i++ )
	{
		if( equali( szMonth, szMonthNames[ i ], 3 ) )
		{
			return i + 1;
		}
	}
	
	return 0;
}

stock __mktime(year, month, day, hour, minute, second)
{
	new timestamp;
	
	#define is_leap_year(%1) (((%1) % 4) == 0 && (((%1) % 100) != 0 || ((%1) % 400) == 0))
	
	for(new i = 1970; i < year; i++)
	{
		timestamp += (31536000 + (is_leap_year(i) ? 86400 : 0));
	}
	
	#define month_days(%1) (((%1) == 2) ? 28 : (((%1) < 8) ? (30 + ((%1) & 1)) : (30 + (((%1) - 7) & 1))))
	
	for(new i = 1; i < month; i++)
	{
		timestamp += ((month_days(i) * 86400) + (is_leap_year(year) ? 86400 : 0));
	}
	
	timestamp += ((day - 1) * 86400);
	timestamp += (hour * 3600);
	timestamp += (minute * 60);
	timestamp += second;
	
	return timestamp;
}